package de.tabuma.pm.projectnavigation.view;

import de.tabuma.pm.view.*;
import de.tabuma.pm.projectnavigation.entity.ProjectNavigation;
import de.tabuma.pm.projectnavigation.entity.ProjectNavigationWrapper;
import de.tabuma.pm.*;
import de.tabuma.lib.tabumaview;
import java.io.PrintWriter;
import java.util.*;
import java.util.logging.*;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author alefen
 */
public class list extends tabumaview {

    private static final Logger log = Logger.getLogger(list.class.getName());

    public list(String servlet, Hashtable parameter) {
        super(servlet, parameter);
    }

    @Override
    public void processRequest(ServletContext context, HttpServletRequest request, HttpServletResponse response, PrintWriter out) {

        ProjectNavigationWrapper pnw = (ProjectNavigationWrapper) request.getAttribute(constants.ProjectNavigationWrapper);

        String link1 = "";
        Enumeration enu = this.getParameter().keys();
        boolean first = true;
        while(enu.hasMoreElements()) {
            String key = (String) enu.nextElement();
            String value = (String) this.getParameter().get(key);
            if(!first) {
                link1 += "&amp;";
            }
            link1 += key+"="+value;
            first = false;
        }

        boolean showDialog = false;
        if(request.getAttribute(constants.ProjectNavigationAdd1)!=null) {
            showDialog = true;
        }

        int navigationId = -1;
        if(request.getAttribute(constants.ProjectNavigationId)!=null) {
            String navId = (String) request.getAttribute(constants.ProjectNavigationId);
            navigationId = Integer.valueOf(navId);
        }

        print(context, request, response, out, pnw, link1, navigationId, true);
    }

    private boolean print(ServletContext context, HttpServletRequest request, HttpServletResponse response, PrintWriter out, ProjectNavigationWrapper pnw, String link1, int navigationId, boolean firstEbene) {

        String remove = "remove element";
        String changeOrder1 = "change order(ascent one step)";
        String changeOrder2 = "change order(descent one step)";
        String externalLink = "configurate, what kind of content is behind the navigation element";
        String addSub = "add Subnavigatation";
        String update = "change element";

        ProjectNavigation pn = (ProjectNavigation) request.getAttribute(constants.ProjectNavigation);

        for(int i=0;i<pnw.getSubElements().size();i++) {

            firstEbene = !firstEbene;

            ProjectNavigationWrapper pnw1 = pnw.getSubElements().get(i);
            String ebene = "";
            if(i+1==pnw.getSubElements().size()&&pnw.getId()!=0) {
                ebene = getEbeneLastElement(pnw1.getEbene());
            }
            else {
                ebene = getEbeneElement(pnw1.getEbene());
            }
            
            boolean showDialog = false;

            if(request.getAttribute(constants.ProjectNavigationUpdate)!=null) {

                if(pn.getId().intValue()==pnw1.getId().intValue()) {
                    showDialog = true;
                }
                
            }

            boolean removeDialog = false;
            if(request.getAttribute(constants.ProjectNavigationRemove)!=null) {
                String navid = (String) request.getAttribute(constants.ProjectNavigationId);
                int navidInt = Integer.valueOf(navid).intValue();
                if(navidInt==pnw1.getId().intValue()) {
                    removeDialog = true;
                }
            }

            boolean contentDialog = false;
            if(request.getAttribute(constants.ProjectNavigationContent)!=null) {
                String navid = (String) request.getAttribute(constants.ProjectNavigationId);
                int navidInt = Integer.valueOf(navid).intValue();
                if(navidInt==pnw1.getId().intValue()) {
                    contentDialog = true;
                }
            }

            if(removeDialog) {
                remove rpnv = new remove(this.getServlet(), this.getParameter());
                rpnv.processRequest(context, request, response, out);
            }
            else {
                if(showDialog) {

                    out.println("<div class=\"navebene_update\">");
                    update upnv = new update(this.getServlet(), this.getParameter());
                    upnv.processRequest(context, request, response, out);

                }
                else {

                    if(contentDialog) {
                        out.println("<div class=\"navebene_content\">");
                        view vpnc = new view(this.getServlet(), this.getParameter());
                        vpnc.processRequest(context, request, response, out);
                    }
                    else {
                        if(firstEbene)
                            out.println("<div class=\"navebene1\">");
                        else
                            out.println("<div class=\"navebene2\">");
                        out.println(ebene);
                        out.println("<a href=\""+this.getServlet()+"?"+link1+"&amp;"+constants.ProjectNavigationId+"="+pnw1.getId()+"&amp;"+constants.ProjectNavigationUpdate+"=true#"+constants.ProjectNavigationA+"\" title=\""+update+"\">"+pnw1.getName()+"</a>");
                    }
                }
                out.println("<a href=\""+this.getServlet()+"?"+link1+"&amp;"+constants.ProjectNavigationId+"="+pnw1.getId()+"&amp;"+constants.ProjectNavigationContent+"=0#"+constants.ProjectNavigationA+"\" class=\"externallink\" id=\"externallink"+pnw1.getId()+"_"+i+"\" title=\""+externalLink+"\"></a>");
                out.println("<a href=\""+this.getServlet()+"?"+link1+"&amp;"+constants.ProjectNavigationId+"="+pnw1.getId()+"&amp;"+constants.ProjectNavigationLevelMinus+"=true#"+constants.ProjectNavigationA+"\" class=\"arrow1\" id=\"arrow1"+pnw1.getId()+"_"+i+"_\" title=\""+changeOrder1+"\"></a>");
                out.println("<a href=\""+this.getServlet()+"?"+link1+"&amp;"+constants.ProjectNavigationId+"="+pnw1.getId()+"&amp;"+constants.ProjectNavigationLevelPlus+"=true#"+constants.ProjectNavigationA+"\" class=\"arrow2\" id=\"arrow2"+pnw1.getId()+"_"+i+"_\" title=\""+changeOrder2+"\"></a>");
                out.println("<a href=\""+this.getServlet()+"?"+link1+"&amp;"+constants.ProjectNavigationId+"="+pnw1.getId()+"&amp;"+constants.ProjectNavigationRemove+"=true#"+constants.ProjectNavigationA+"\" class=\"remove_cross\" id=\"remove_cross"+pnw1.getId()+"_"+i+"_\" title=\""+remove+"\"></a>");

                boolean createDialog = false;

                if(navigationId==pnw1.getId()) {
                    if(request.getAttribute(constants.ProjectNavigationAdd)!=null) {
                        createDialog = true;
                    }
                }
                if(createDialog) {
                    out.println("</div>");
                    print(context, request, response, out, pnw1, link1, navigationId, firstEbene);
                    out.println("<div class=\"navebene_create\">");
                    out.println(getEbeneLastElement(pnw1.getEbene()+1));
                    create cpnv = new create(this.getServlet(), this.getParameter());
                    cpnv.processRequest(context, request, response, out);
                    out.println("</div>");

                }
                else {

                    out.println("<a href=\""+this.getServlet()+"?"+link1+"&amp;"+constants.ProjectNavigationAdd+"=true&amp;"+constants.ProjectNavigationId+"="+pnw1.getId()+"\" class=\"navigation_plus\" id=\"navigation_plus"+pnw1.getId()+"_"+i+"\" title=\""+addSub+"\"></a>");
                    out.println("</div>");
                    firstEbene = print(context, request, response, out, pnw1, link1, navigationId, firstEbene);

                }
            }

        }

        if(pnw.getId()==0) {
            firstEbene = !firstEbene;
            if(navigationId != 0) {
                if(firstEbene)
                    out.println("<div class=\"navebene1\">");
                else
                    out.println("<div class=\"navebene2\">");
                out.println(getEbeneLastElement(pnw.getEbene()+1));

                out.println("<a href=\""+this.getServlet()+"?"+link1+"&amp;"+constants.ProjectNavigationAdd+"=true&amp;"+constants.ProjectNavigationId+"="+pnw.getId()+"#"+constants.ProjectNavigationA+"\" class=\"navigation_plus\" id=\"last_navigation_plus\"></a>");

            }
            else {
                out.println("<div class=\"navebene_create\">");
                out.println(getEbeneLastElement(pnw.getEbene()+1));
                create cpnv = new create(this.getServlet(), this.getParameter());
                cpnv.processRequest(context, request, response, out);
            }
            out.println("</div>");
        }

        return firstEbene;

    }

    private static String getEbene(int ebene) {
        String ebeneStr = "";
        for(int i=0;i<ebene;i++) {
            ebeneStr += "<div class=\"navigationLine\"></div>";
        }
        return ebeneStr;
    }

    public static String getEbeneElement(int ebene) {
        String ebeneStr = getEbene(ebene);
        ebeneStr += "<div class=\"navigationElement\"></div>";
        return ebeneStr;
    }

    public static String getEbeneLastElement(int ebene) {
        String ebeneStr = getEbene(ebene);
        ebeneStr += "<div class=\"navigationLastElement\"></div>";
        return ebeneStr;
    }
    
}